//
//  ChooseServiceObjViewController.m
//  Utils
//
//  Created by enghou on 2018/6/8.
//  Copyright © 2018年 xyxorigation. All rights reserved.
//

#import "ChooseServiceObjViewController.h"
#import "ServiceMemberCell.h"
#import "NetWorkerManager.h"
#import "ChooseObjectTypeViewController.h"
#import "SubTypeItemModel.h"
#import "TypeListModel.h"
#import "RefreshedTableView.h"
@interface ChooseServiceObjViewController ()<UITextFieldDelegate,TotalProtocol,beginRefresh>
@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;
@property (weak, nonatomic) IBOutlet UILabel *objTypeLabel;
@property (weak, nonatomic) IBOutlet UILabel *objSubTypeLabel;
@property (weak, nonatomic) IBOutlet UITextField *nameField;

@property(nonatomic,copy)NSString *objTypeId;
@property(nonatomic,copy)NSString *objSubTypeId;

@property(nonatomic,weak)NSString *updateString;
@property(nonatomic,weak)UILabel *updateLbl;
@property (strong, nonatomic) IBOutletCollection(UITableViewCell) NSArray *cells;

@end

@implementation ChooseServiceObjViewController
@synthesize objTypeId;
@synthesize objSubTypeId;

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView registerNib:[UINib nibWithNibName:@"ServiceMemberCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"ServiceMemberCell"];
    [self.nameField addTarget:self action:@selector(change:) forControlEvents:UIControlEventValueChanged];
    [self beginRefresh];
    RefreshedTableView *refresh = (RefreshedTableView *)self.tableView;
    refresh.refreshDelegate = self;
    
}

-(void)change:(UITextField *)field{
    [self beginRefresh];
}

-(void)beginRefresh{
    [self beginLoading];
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    if (objTypeId) {
        [param setObject:objTypeId forKey:@"serviceObjectVo.objectBigType"];
    }
    if (objSubTypeId) {
        [param setObject:objSubTypeId forKey:@"serviceObjectVo.objectType"];
    }
    [param setObject:_nameField.text forKey:@"serviceObjectVo.name"];
    [param setObject:@"1" forKey:@"page"];
    [param setObject:@"10" forKey:@"rows"];
    [[NetWorkerManager sharedInstance]postWithPath:@"/mobile/comprehensiveGovernance/serviceRecord/findPopulationsForServiceRecord.action" parameter:param completeBlock:^(NSDictionary *source) {
        [self endLoading];
        BaseModel *model = [[BaseModel alloc]initWithSourceDict:source listItemClass:NSClassFromString(@"ObjectsItemModel")];
        if (model.success) {
            self.page = 1;
            self.source = [model.list mutableCopy];
            [self.tableView reloadData];
        }
    } failBlock:^(NSInteger code) {
        [self endLoading];
    }];
}

-(void)loadNext{
    [self beginLoading];
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    if (objTypeId) {
        [param setObject:objTypeId forKey:@"serviceObjectVo.objectBigType"];
    }
    if (objSubTypeId) {
        [param setObject:objSubTypeId forKey:@"serviceObjectVo.objectType"];
    }
    [param setObject:_nameField.text forKey:@"serviceObjectVo.name"];
    [param setObject:[NSString stringWithFormat:@"%li",self.page+1] forKey:@"page"];
    [param setObject:@"10" forKey:@"rows"];
    [[NetWorkerManager sharedInstance]postWithPath:@"/mobile/comprehensiveGovernance/serviceRecord/findPopulationsForServiceRecord.action" parameter:param completeBlock:^(NSDictionary *source) {
        [self endLoading];
        BaseModel *model = [[BaseModel alloc]initWithSourceDict:source listItemClass:NSClassFromString(@"ObjectsItemModel")];
        if (model.success) {
            if (model.list.count > 0) {
                self.page++;
            }
            [self.source addObjectsFromArray:model.list];
            [self.tableView reloadData];
        }
    } failBlock:^(NSInteger code) {
        [self endLoading];
    }];
}

#pragma mark - Table view data source

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    switch (section) {
        case 0:
            return 3;
            break;
        case 1:
            return self.source.count;
            break;
        default:
            return 0;
            break;
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    switch (section) {
        case 0:
            return nil;
            break;
            
        default:
        {
            UIView *header = [[UIView alloc]init];
            header.backgroundColor = [UIColor defaultBackgroundColor];
            UILabel *nameLbl = [[UILabel alloc]init];
            nameLbl.text = @"姓名";
            nameLbl.textAlignment = NSTextAlignmentCenter;
            nameLbl.frame = CGRectMake(0, 0, self.view.width / 2, 44);
            [header addSubview:nameLbl];
            UILabel *teamLbl = [[UILabel alloc]init];
            teamLbl.text = @"身份证号";
            teamLbl.textAlignment = NSTextAlignmentCenter;
            teamLbl.frame = CGRectMake(self.view.width / 2, 0, self.view.width / 2, 44);
            [header addSubview:teamLbl];
            return header;
        }
            break;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    switch (section) {
        case 0:
            return 0.1;
            break;
        default:
            return 44;
            break;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        if (indexPath.row == 0 || indexPath.row == 1) {
            if (indexPath.row == 0) {
                self.updateString = objTypeId;
                self.updateLbl = self.objTypeLabel;
            }else{
                self.updateString = objSubTypeId;
                self.updateLbl = self.objSubTypeLabel;
            }
            ChooseObjectTypeViewController *vc = [[ChooseObjectTypeViewController alloc]init];
            vc.theDelete = self;
            vc.needParentInfo = YES;
            vc.itemCls = @"ServiceBigTypeModel";
            vc.path = @"/mobile/comprehensiveGovernance/serviceRecord/findPopulationBigType.action";
            [self push:vc];
        }
    }else if (indexPath.section == 1){
        if ([self.theDelegate respondsToSelector:@selector(fetchModel:)]) {
            id model = self.source[indexPath.row];
            [self.theDelegate fetchModel:model];
            [self pop];
        }
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.section) {
        case 0:
            return self.cells[indexPath.row];
            break;
            
        default:
        {
            ServiceMemberCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ServiceMemberCell"];
            if (indexPath.row < self.source.count) {
                cell.model = self.source[indexPath.row];
            }
            return cell;
        }
            
            break;
    }
}

- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 1) {
        return [super tableView:tableView indentationLevelForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]];
    }
    return [super tableView:tableView indentationLevelForRowAtIndexPath:indexPath];
}

#pragma mark - HandleSubType
-(void)handleSubModel:(SubTypeItemModel *)model{
    
}

-(void)fetchModel:(id)model{
    if ([model isKindOfClass:[NSDictionary class]]) {
        NSDictionary *dictionary = (NSDictionary *)model;
        TypeListModel *father = dictionary[@"parent"];
        SubTypeItemModel *sub = dictionary[@"sub"];
        self.objTypeLabel.text = father.name;
        self.objSubTypeLabel.text = sub.name;
        self.objTypeId = father.tid;
        self.objSubTypeId = sub.subTid;
        [self beginRefresh];
    }
}

@end
